package databasetest.zx.com.databasetest.db;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import databasetest.zx.com.databasetest.AppApplication;
import databasetest.zx.com.databasetest.model.UserModel;

import static databasetest.zx.com.databasetest.db.MyDataBaseHelper.USER_TABLE_NAME;
import static databasetest.zx.com.databasetest.model.UserModel.Columns._ID;

/**
 * Created by zhoux on 2017/12/11.
 * 注释：
 */

public class UserDBController {
    String TAG = "UserDBController";
    private final SQLiteDatabase db;

    public UserDBController() {
//        MyDataBaseHelper openHelper = new MyDataBaseHelper(AppApplication.CONTEXT, "zxdbtest.db");
        db = DBManager.getInstance(AppApplication.CONTEXT, "zxdbtest.db").openDatabase();
    }


    public UserModel addOne(UserModel model) {
        try {
            //
            long _id = db.insert(USER_TABLE_NAME, null, model.toContentValues());
            final boolean succeed = _id != -1;
            model._id = _id;
            return succeed ? model : null;

        } catch (Exception e) {

        } finally {
            closeDatabase(null);
        }
        return null;
    }

    /**
     * 当insert第二个参数传入null时候，会报以下错误：
     * * android.database.sqlite.SQLiteException: near "null": syntax error (code 1): , while compiling: INSERT INTO USER_TABLE(null) VALUES (NULL)
     * 原因：见源码：
     *
     * @see android.database.sqlite.SQLiteDatabase#insertWithOnConflict
     * @see android.database.sqlite.SQLiteDatabase#execSQL
     * <p>
     * 会执行此段sql：INSERT INTO USER_TABLE(null) VALUES (NULL)
     * 不会通过
     * <p>
     * 如果传入一个字段名：
     * 会执行以下SQL：
     * INSERT INTO USER_TABLE(_id) VALUES (NULL)
     * 可以通过
     */
    public UserModel addOneNull(UserModel model) {
        try {
            //
//            long _id = db.insert(USER_TABLE_NAME, null, null);//android.database.sqlite.SQLiteException: near "null": syntax error (code 1): , while compiling: INSERT INTO USER_TABLE(null) VALUES (NULL)
            long _id = db.insert(USER_TABLE_NAME, _ID, null);
            final boolean succeed = _id != -1;
            model._id = _id;
            return succeed ? model : null;

        } catch (Exception e) {

        } finally {
            closeDatabase(null);
        }
        return null;
    }

    public int addOneByExecSql(UserModel model) {
        try {
            //
            StringBuilder sql = new StringBuilder();
            sql.append("INSERT");
            sql.append(" INTO ");
            sql.append(USER_TABLE_NAME);
            sql.append('(');
            sql.append(UserModel.Columns.NAME);
            sql.append(',');
            sql.append(UserModel.Columns.AGE);
            sql.append(')');
            sql.append(" VALUES (");
            sql.append(" '");
            sql.append(model.name);
            sql.append(" '");
            sql.append(',');
            sql.append(" '");
            sql.append(model.age);
            sql.append(" '");
            sql.append(')');
            String sql2 = "insert into " + USER_TABLE_NAME + "(" + UserModel.Columns.NAME + "," + UserModel.Columns.AGE + ") values ('" + model.name + "','" + model.age + "')";
            Log.e(TAG, sql.toString());
            db.execSQL(sql.toString());
            return 1;

        } catch (Exception e) {

        } finally {
            closeDatabase(null);
        }
        return -1;

    }
//    public UserModel getOneByName(String name) {
//
//    }

    public void closeDatabase(Cursor cursor) {
        if (null != cursor) {
            cursor.close();
        }
    }


}
